#include "tree.h"
//template <class Ty>
//void InitTree(treenode<Ty> const*& pRoot, size_t Depth)
//{
//    pRoot = new treenode<int>;
//    pRoot->value = 0;
//    if (Depth > 0)
//    {
//for(auto i=pRoot->)
//    }
//    else
//    {
//        pRoot->Lchild = pRoot->Rchild = nullptr;
//    }
//    return;
//}
template<class Ty>
treenode<Ty>::~treenode()
{
    clear();
}

template<class Ty>
treenode<Ty>::treenode()
{
    PRoot = nullptr;
}

template<class Ty>
treenode<Ty>::treenode(size_t nBranches, size_t Depth)
{

}

template<class Ty>
bool treenode<Ty>::find(Ty const& FindData)
{
    return false;
}

template<class Ty>
bool treenode<Ty>::edit(Ty const& OldData, Ty const& NewData)
{
    return false;
}

template<class Ty>
void treenode<Ty>::erase(treenode<Ty>* Node)
{
    _clear(Node);
}

template<class Ty>
void treenode<Ty>::_clear(treenode<Ty>* root)
{
    if (root != nullptr)
    {
        if (root->Childlist.size() > 0)
        {
            for (auto i = root->Childlist.begin(); i < root->Childlist.end(); i++)
            {
                _clear(root->Childlist[i]);
            }
        }
        delete root;
    }
}

template<class Ty>
treenode<Ty>* treenode<Ty>::_find(Ty const& FindData)
{
    return nullptr;
}

template<class Ty>
void treenode<Ty>::clear()
{
    _clear(PRoot);
}

template<class Ty>
void BiTree_Arr<Ty>::DisplayElem(size_t index)
{
    if (index < len - 1)
    {
        printf("%d", vals[index]);
        DisplayElem(2 * index + 1);
        DisplayElem(2 * index + 2);
    }
}

template<class Ty>
void BiTree_Arr<Ty>::_clear()
{
    if(vals)
        delete[] vals;
    vals = nullptr;
    len = 0;
}

template<class Ty>
BiTree_Arr<Ty>::BiTree_Arr(size_t _Depth)
{
    vals = new Ty[2 * _Depth - 1];
    len = 2 * _Depth - 1;
    for (int i = 0; i < 2 * _Depth - 1; i++)
    {
        vals[i] = (Ty)i;
    }
}

template<class Ty>
BiTree_Arr<Ty>::~BiTree_Arr()
{
    clear();
}

template<class Ty>
void BiTree_Arr<Ty>::clear()
{
    _clear();
}

template<class Ty>
void BiTree_Arr<Ty>::DisplayTree()
{

}

template<class Ty>
void BiTree_Arr<Ty>::initTree( Ty const arr[] , size_t len)
{
    for (int i = 0; i < len; i++)
    {
        vals[i] = arr[i];
    }
}

template<class Ty>
bool BiTree_Arr<Ty>::FindVal(Ty const& FindData) const
{
    return _FindVal(FindData) != -1;
}

template<class Ty>
int BiTree_Arr<Ty>::_FindVal(Ty const& FindData) const
{
    for (int i = 0; i < len; i++)
    {
        if (vals[i] == FindData)
            return i;
    }
    return -1;
}

template<class Ty>
bool BiTree<Ty>::_find()
{
    return false;
}

template<class Ty>
void BiTree<Ty>::_clear()
{
    if(pRoot)
    {
        erase(pRoot);
    }
}



void BiTree<int>::_initINTBiTree(node<int>* pRoot, int ValofLastBranch)
{
    static int i = 0;
    
    if (i< ValofLastBranch)
    {
        i++;
        pRoot->LChild = new node<int>;
        pRoot->LChild->Val = i;
        pRoot->LChild->LChild = pRoot->LChild->RChild = nullptr;
    }
    if (i < ValofLastBranch)
    {
        i++;
        pRoot->RChild = new node<int>;
        pRoot->RChild->Val = i;
        pRoot->RChild->LChild = pRoot->RChild->RChild = nullptr;
    }
    if (i < ValofLastBranch)
    {
        _initINTBiTree(pRoot->LChild, ValofLastBranch);
    }
    if (i < ValofLastBranch)
    {
        _initINTBiTree(pRoot->RChild, ValofLastBranch);
    }
}

void BiTree<int>::initINTBiTree(int ValofLastBranch)
{
    if (pRoot!=nullptr)
    {
        _clear();
    }
    pRoot = new node<int>;
    pRoot->Val = 0;
    _initINTBiTree(pRoot, ValofLastBranch);
}

template<class Ty>
void BiTree<Ty>::clear()
{
    _clear();
}

template<class Ty>
void BiTree<Ty>::find(Ty const& FindData)
{
}

template<class Ty>
void BiTree<Ty>::erase(node<Ty> *& RootToErase)
{
    if (RootToErase != nullptr)
    {
        if (RootToErase->LChild != nullptr)
        {
            erase(RootToErase->LChild);
        }
        if (RootToErase->RChild != nullptr)
        {
            erase(RootToErase->RChild);
        }
        delete RootToErase;
        RootToErase = nullptr;
    }
}

